﻿using BW.Mes.WinForms.APi;
using BW.Mes.WinForms.WareHouse.Dto;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BW.Mes.WinForms.WareHouse.Libraryrecord
{
    public partial class SerialNumberForm : DevExpress.XtraEditors.XtraForm
    {

        public ApiPaing<SerialNumberFormModel>  serial;
        public SerialNumberForm()
        {
            InitializeComponent();
            this.Load += CreateLibraryrecordForm_Load;

            // 注册分页控件事件
            userControl11.ClickPageButtonEvent += UserControl1_ClickPageButtonEvent;
            userControl11.ChangedPageSizeEvent += UserControl1_ChangedPageSizeEvent;
            userControl11.JumpPageEvent += UserControl1_JumpPageEvent;
            this.Load += SerialNumberForm_Load;
        }

        private void CreateLibraryrecordForm_Load(object sender, EventArgs e)
        {
            ReceiptsLoadLibraryrecord();
        }



        private async void SerialNumberForm_Load(object sender, EventArgs e)
        {
            await GetSerialNumberForm();
            DisplayPagedData();
        }



        /// <summary>
        /// 获取来料标签打印记录
        /// </summary>
        public async Task GetSerialNumberForm()
        {
            string queryParms = "?";
            if (textEdit1.Text != "")
            {
                queryParms += $"Oddnumber={textEdit1.Text}&";
            }
            if (gridLookUpEdit1.EditValue != null)
            {
                queryParms += $"Receiptstype={gridLookUpEdit1.EditValue}&";
            }
           
            if (dateEdit1.EditValue != null && dateEdit2.EditValue != null)
            {
                queryParms += $"BeginTime={dateEdit1.EditValue}&EndTime={dateEdit2.EditValue}&";
            }


            var result = await HttpClientHepler.RequsetUrl(RequestType.get, MethodGolab.SHOW_SERIAL + queryParms);

            //反序列化
            serial = JsonConvert.DeserializeObject<ApiPaing<SerialNumberFormModel>>(result);
            if (serial.code == 200)
            {
                this.gridControl1.DataSource = serial.pageDatas;
            }

            //// 设置总记录数和总页数
            userControl11.TotalRows = serial.totalCount;
            userControl11.TotalPages = (int)Math.Ceiling((double)serial.totalCount / userControl11.PageSize);

            // 更新页码信息标签
            userControl11.PageInfo.Text = $"第{userControl11.CurrentPage}/{userControl11.TotalPages}页";


            // 确保清除旧数据源并重新设置
            gridControl1.DataSource = null;
            gridControl1.DataSource = serial;

            // 确保网格视图显示数据
            gridView1.PopulateColumns();
            gridView1.BestFitColumns();
            gridView1.RefreshData();

        }



        // 显示分页后的数据
        private void DisplayPagedData()
        {
            if (serial == null || serial.totalCount == 0)
                return;

            int startIndex = (userControl11.CurrentPage - 1) * userControl11.PageSize;
            int count = Math.Min(userControl11.PageSize, serial.totalCount - startIndex);

            if (startIndex < 0 || count <= 0)
                return;

            // 获取当前页的数据
            var pagedData = serial.pageDatas.Skip(startIndex).Take(count).ToList();

            // 绑定到GridControl
            gridControl1.DataSource = null;
            gridControl1.DataSource = pagedData;

            // 刷新网格视图
            gridView1.BestFitColumns();
            gridView1.RefreshData();
        }

        // 页码按钮点击事件处理
        private void UserControl1_ClickPageButtonEvent(int currentPage)
        {
            DisplayPagedData();
        }

        // 每页显示条数改变事件处理
        private void UserControl1_ChangedPageSizeEvent()
        {
            // 重新计算总页数
            userControl11.TotalPages = (int)Math.Ceiling((double)serial.totalCount / userControl11.PageSize);
            userControl11.PageInfo.Text = $"第{userControl11.CurrentPage}/{userControl11.TotalPages}页";
            DisplayPagedData();
        }

        // 跳转页面事件处理
        private void UserControl1_JumpPageEvent(int jumpPage)
        {
            DisplayPagedData();
        }






        /// <summary>
        /// 单据类型
        /// </summary>
        public class ReceiptsBankModel
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }

        /// <summary>
        /// 单据类型列表
        /// </summary>
        private void ReceiptsLoadLibraryrecord()
        {
            // 创建物料数据列表
            List<ReceiptsBankModel> materialList = new List<ReceiptsBankModel>
            {
                new ReceiptsBankModel { Id = "采购入库", Name = "采购入库" },
                new ReceiptsBankModel { Id = "生产入库", Name = "生产入库" },
                new ReceiptsBankModel { Id = "采购出库", Name = "采购出库" },
                new ReceiptsBankModel { Id = "生产出库", Name = "生产出库" },
                new ReceiptsBankModel { Id = "供应商出库", Name = "供应商出库" },
                new ReceiptsBankModel { Id = "供应商入库", Name = "供应商入库" },
                // 可以继续添加更多物料数据
            };

            // 设置数据源
            gridLookUpEdit1.Properties.DataSource = materialList;
            gridLookUpEdit1.Properties.DisplayMember = "Name";
            gridLookUpEdit1.Properties.ValueMember = "Id";

            // 配置下拉框显示列
            gridLookUpEdit1.Properties.View.Columns.Clear();
            gridLookUpEdit1.Properties.View.Columns.Add(
                new DevExpress.XtraGrid.Columns.GridColumn
                {
                    FieldName = "Name",
                    Caption = "单据类型名称",
                    Visible = true
                }
            );

            // 设置默认提示文本
            gridLookUpEdit1.Properties.NullText = "请选择单据类型";
        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            await GetSerialNumberForm();
            DisplayPagedData();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            textEdit1.Text = "";
            gridLookUpEdit1.EditValue = "";
            dateEdit1.EditValue = "";
            dateEdit2.EditValue = "";

        }
    }
}